package cpu.footprint.api;

import android.content.Context;
import android.content.SharedPreferences;

import cpu.footprint.bean.User;

public class LocalStorage {
	private static SharedPreferences getSharedPreferences(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"userModel", Context.MODE_PRIVATE);
		return sharedPreferences;
	}

	public static void saveString(Context context, String key, String value) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		sharedPreferences.edit().putString(key, value).commit();
	}
	public static String getString(Context context, String key) {
		return getSharedPreferences(context).getString(key, "");
	}
	/*
	*存储User实体
	 */
	public static void saveUserModel(Context context, User model) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		SharedPreferences.Editor editor=sharedPreferences.edit();
		editor.putString("id", model.getId()+"");
		editor.putString("mobilePhone", model.getMobilePhone());
		editor.putString("password", model.getPassword());
		editor.putString("headPicture", model.getHeadPicture());
		editor.putString("nickName", model.getNickName());
		editor.putString("realName", model.getRealName());
		editor.putString("sex", model.getSex());
		editor.putString("address", model.getAddress());
		editor.putString("birthday", model.getBirthday());
		editor.putString("email", model.getEmail());
		editor.putString("lastLoginTime", model.getLastLoginTime()+"");
		editor.putString("registerTime", model.getRegisterTime()+"");
		editor.putString("token", model.getToken());
		editor.putString("individualSignature", model.getIndividualSignature());
		editor.putString("currentlyActiveId", model.getCurrentlyActiveId()+"");
		editor.commit();
	}
	/*
	*清除User实体
 	*/
	public static void removeUserModel(Context context) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		SharedPreferences.Editor editor=sharedPreferences.edit();
		editor.putString("id", "");
		editor.putString("mobilePhone","");
		editor.putString("password","");
		editor.putString("headPicture","");
		editor.putString("nickName","");
		editor.putString("realName","");
		editor.putString("sex","");
		editor.putString("age","");
		editor.putString("address","");
		editor.putString("birthday","");
		editor.putString("email","");
		editor.putString("lastLoginTime","");
		editor.putString("registerTime","");
		editor.putString("token","");
		editor.putString("individualSignature","");
		editor.putString("currentlyActiveId","");
		editor.commit();
	}

	/*
	*读取User实体
	 */
	public static User getUserModel(Context context) {
		User model=new User();
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		model.setId(Integer.parseInt(sharedPreferences.getString("id", "0")));
		model.setMobilePhone(sharedPreferences.getString("mobilePhone",""));
		model.setPassword(sharedPreferences.getString("password", ""));
		model.setHeadPicture(sharedPreferences.getString("headPicture",""));
		model.setNickName(sharedPreferences.getString("nickName",""));
		model.setRealName(sharedPreferences.getString("realName",""));
		model.setSex(sharedPreferences.getString("sex",""));
		model.setAddress(sharedPreferences.getString("address",""));
		model.setBirthday(sharedPreferences.getString("birthday",""));
		model.setEmail(sharedPreferences.getString("email",""));
		model.setToken(sharedPreferences.getString("token",""));
		model.setIndividualSignature(sharedPreferences.getString("individualSignature",""));
		model.setIndividualSignature(sharedPreferences.getString("currentlyActiveId","0"));
		return model;
	}

}
